package com.music.service.impl;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Service;
import com.laoxuai.ibatis.util.Example;
import com.laoxuai.ibatis.util.PageInfo;

import com.laoxuai.util.StringUtils;
import com.music.dao.RecordDao;
import com.music.pojo.Record;
import com.music.service.RecordService;

import java.sql.SQLException;
import java.util.List;

@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordDao recordDao;

    @Override
    public int insert(Record record) {
        try {
            return recordDao.insert(record);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        try {
            return recordDao.delete(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Record record) {
        try {
            return recordDao.update(record);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Record getByPrimaryKey(Integer id) {
        try {
            return recordDao.getByPrimaryKey(id);
        } catch (SQLException e) {
        e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Record> listPage(Record record, int page, int size) {
        try {
            return recordDao.listPage(record, page, size);
        } catch (SQLException throwables) {
        throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<Record> listPageInfo(Record record, int page, int size) {
        try {
            return recordDao.listPageInfo(record, page, size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Record> selectByExample(Record record) {
        Example example = createExmaple(record);
        try {
            return recordDao.selectByExample(example);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Record getRecord(Integer userId, Integer musicId) {
        try {
            return recordDao.getRecord(userId,musicId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private Example createExmaple(Record record) {
        Example example = new Example(Record.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO
        if(record != null){
            if(!StringUtils.isEmpty(record.getId())){
                criteria.andEqualTo("id", record.getId());
            }
            if(!StringUtils.isEmpty(record.getUserId())){
                criteria.andEqualTo("userId", record.getUserId());
            }
            if(!StringUtils.isEmpty(record.getMusicId())){
                criteria.andEqualTo("musicId", record.getMusicId());
            }
            if(!StringUtils.isEmpty(record.getPlayCount())){
                criteria.andEqualTo("playCount", record.getPlayCount());
            }
        }
        return example;
    }

    /**
     * Record 构建查询对象
     * @param record
     * @return
     */
    public Example createExample(Record record){
        Example example = new Example(Record.class);
        Example.Criteria criteria = example.createCriteria();
        if(record != null){
            if(!StringUtils.isEmpty(record.getId())){
                criteria.andEqualTo("id", record.getId());
            }
            if(!StringUtils.isEmpty(record.getUserId())){
                criteria.andEqualTo("userId", record.getUserId());
            }
            if(!StringUtils.isEmpty(record.getMusicId())){
                criteria.andEqualTo("musicId", record.getMusicId());
            }
            if(!StringUtils.isEmpty(record.getPlayCount())){
                criteria.andEqualTo("playCount", record.getPlayCount());
            }
        }
        return example;
    }
}
